package com.langxin.erp.action;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.langxin.erp.biz.UserBiz;
import com.langxin.erp.model.UserInfo;
import com.langxin.erp.util.constant.SiteConstant;
import com.langxin.erp.util.web.JsonUtils;

/**
 * 用户登录/登出
 * @author guqj
 *
 */
@Controller
public class AccountAction extends BaseAction{

	@Resource
	private UserBiz   userBiz;
	
	/**
	 * 登录
	 * @param model
	 * @param userInfo
	 * @return
	 */
	@RequestMapping(value="/", method = RequestMethod.GET)
	public String loginGet(Model model, UserInfo userInfo){
		model.addAttribute("userInfo", userInfo);
		return "login.jsp";
	}
	
	/**
	 * 登录
	 * @param model
	 * @param adminInfo
	 * @return
	 */
	@RequestMapping(value="login", method = RequestMethod.POST)
	@ResponseBody
	public String loginPost(Model model, UserInfo userInfo, BindingResult error,HttpServletResponse response){
		if(error.hasErrors()){
			return JsonUtils.toFormJson(SiteConstant.FAIL, "账号和密码错误，登录失败！");
		}
		if(userBiz.login(userInfo)){
			return JsonUtils.toFormJson(SiteConstant.SUC, "登录成功");
		}
		return JsonUtils.toFormJson(SiteConstant.FAIL, "账号和密码错误，登录失败！");
	}
	
	
	/**
	 * 退出登录
	 * @return
	 */
	@RequestMapping(value="logout", method = RequestMethod.GET)
	public String logout(){
		userBiz.logout();
		return "redirect:/";
	}
	
	/**
	 * 密码修改
	 * @param param
	 * @return
	 */
	@RequestMapping(value="password/update", method = RequestMethod.POST)
	@ResponseBody
	public String password(UserInfo userInfo){
		if(userBiz.updatePasswd(userInfo)){
			return JsonUtils.toFormJson(SiteConstant.SUC, "修改成功");
		}
		return JsonUtils.toFormJson(SiteConstant.FAIL, "修改失败！");
	}
}
